package cn.consult.sys.service.impl;

import cn.consult.sys.dao.TPlaceFloorDao;
import cn.consult.sys.dao.TPlaceRoomDao;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.entity.TPlaceFloor;
import cn.consult.sys.entity.TPlaceRoom;
import cn.consult.sys.service.TPlaceFloorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 楼层(TPlaceFloor)表服务实现类
 *
 * @author makejava
 * @since 2024-04-01 08:36:15
 */
@Service("tPlaceFloorService")
public class TPlaceFloorServiceImpl extends ServiceImpl<TPlaceFloorDao, TPlaceFloor> implements TPlaceFloorService {

    @Autowired
    TPlaceRoomDao roomDao;

    @Override
    public List<TPlaceFloor> getFloosByBulidingId(Long buildingId) {
        LambdaQueryWrapper<TPlaceFloor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TPlaceFloor::getFloorBuildingid, buildingId);
        List<TPlaceFloor> tPlaceFloors = this.baseMapper.selectList(queryWrapper);
        return tPlaceFloors;
    }

    @Override
    public List<TPlaceFloor> selectByBuildingNameOrNumber(Map<String, Object> params) {

        LambdaQueryWrapper<TPlaceFloor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(params.get("floorSerialnumber") != null && params.get("floorSerialnumber") != "", TPlaceFloor::getFloorSerialnumber, params.get("floorSerialnumber"));
        queryWrapper.like(params.get("floorName") != null && params.get("floorName") != "", TPlaceFloor::getFloorName, params.get("floorName"));
        queryWrapper.eq(params.get("bulidingId") != null && params.get("bulidingId") != "", TPlaceFloor::getFloorBuildingid, params.get("bulidingId"));

        List<TPlaceFloor> tPlaceFloors = this.baseMapper.selectList(queryWrapper);
        if (tPlaceFloors != null) {
            for (TPlaceFloor tPlaceFloor : tPlaceFloors) {
                LambdaQueryWrapper<TPlaceRoom> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(TPlaceRoom::getRoomFloorid, tPlaceFloor.getId());
                Integer i = roomDao.selectCount(wrapper);
                tPlaceFloor.setFloorRoomnumber(i);
                this.baseMapper.updateById(tPlaceFloor);
            }

            return tPlaceFloors;
        }
        return null;
    }

    @Override
    public boolean updateFloor(TPlaceFloor tPlaceFloor) {
        int i = this.baseMapper.updateById(tPlaceFloor);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean flagName(String name, String bulidingId) {
        LambdaQueryWrapper<TPlaceFloor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TPlaceFloor::getFloorSerialnumber, name);
        lambdaQueryWrapper.eq(TPlaceFloor::getFloorBuildingid, bulidingId);
        List<TPlaceFloor> tPlaceFloors = this.baseMapper.selectList(lambdaQueryWrapper);
        if (tPlaceFloors.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteFloorById(Long id) {
        LambdaQueryWrapper<TPlaceRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TPlaceRoom::getRoomFloorid, id);
        List<TPlaceRoom> tPlaceRooms = roomDao.selectList(lambdaQueryWrapper);
        if (tPlaceRooms.size() > 0) {
            roomDao.delete(new LambdaQueryWrapper<TPlaceRoom>().eq(TPlaceRoom::getRoomFloorid, id));
        }

        if (this.baseMapper.deleteById(id) > 0) {
            return true;
        }
        return true;
    }

    @Override
    public boolean saveFloor(TPlaceFloor tPlaceFloor) {
        int insert = this.baseMapper.insert(tPlaceFloor);
        if (insert > 0) {
            if (tPlaceFloor.getFloorRoomnumber() != null) {
                if (tPlaceFloor.getFloorRoomnumber() > 0) {
                    for (Integer i = 0; i < tPlaceFloor.getFloorRoomnumber(); i++) {
                        TPlaceRoom room = new TPlaceRoom();
                        room.setRoomFloorid(String.valueOf(tPlaceFloor.getId()));
                        room.setRoomSerialnumber(String.valueOf(i + 1));
                        room.setRoomName(tPlaceFloor.getFloorName()+String.valueOf(i + 1)+"号");
                        roomDao.insert(room);
                    }
                }
            }


        }

        return false;
    }
}

